package cn.manager.travel.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class CharchaterFilter implements Filter {
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
	    //将父接口转换为子接口
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        //获取请求的方法
        String method = req.getMethod();
        //解决post请求中文数据乱码问题
        if (method.equalsIgnoreCase("post")){
            req.setCharacterEncoding("utf-8");
        }
        //处理响应乱码
        resp.setContentType("text/html;charset=utf-8");
        chain.doFilter(req,resp);
    }
	
	public void init(FilterConfig config) throws ServletException {
	}
	
	public void destroy() {
	}
	
}
